package com.shiro.common.shiro;

import com.shiro.common.entity.User;
import com.shiro.common.entity.base.NameEntity;
import com.shiro.repsoitory.PermissionRepository;
import com.shiro.repsoitory.RoleRepository;
import com.shiro.repsoitory.UserRepository;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: dengpeng
 * Date: 2018/7/5
 * Time: 14:08
 * Description: No Description
 */
@Component
public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        String username = (String) principals.getPrimaryPrincipal();
        User user = userRepository.findByName(username);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        Set<String> roles = user.getRoleList().stream().map(NameEntity::getName).collect(Collectors.toSet());

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        return null;
    }
}
